package com.itheima.mongo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.mongoservice.UserLocationServiceApi;
import com.itheima.pojo.mongo.UserLocation;
import com.itheima.vo.mongo.UserLocationVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserLocationServiceApiImpl implements UserLocationServiceApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Map<String, Double> getUserLocation(Long uid) {
        Query query = Query.query(Criteria.where("userId").is(uid));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //返回
        if (ObjectUtil.isNull(userLocation)){
            //设置默认值(北京天安门)
            Map<String, Double> map = new HashMap<>();
            map.put("x", 116.38);
            map.put("y", 39.90);
            return map;
        }
        //用户位置
        Map<String, Double> map = new HashMap<>();
        map.put("x", userLocation.getLocation().getX());
        map.put("y", userLocation.getLocation().getY());
        return map;
    }

    @Override
    public List<Long> getSearchUser(Long uid, Integer distance) {
        //获取登录者位置
        Map<String, Double> map = getUserLocation(uid);
        //获取圆心
        GeoJsonPoint userLocation = new GeoJsonPoint(map.get("x"), map.get("y"));
        //绘制半径
        Distance userDistance = new Distance(distance/1000, Metrics.KILOMETERS);
        //绘制圆形
        Circle circle = new Circle(userLocation, userDistance);
        //查询
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(query, UserLocation.class);
        //返回ids
        return CollUtil.getFieldValues(userLocationList, "userId", Long.class);
    }

    @Override
    public boolean isLocationExist(Long uid) {
        Query query = Query.query(Criteria.where("userId").is(uid));
        return mongoTemplate.exists(query, UserLocation.class);
    }

    @Override
    public UserLocationVo getUserLocationVo(Long uid) {
        UserLocation userLocation = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(uid)), UserLocation.class);
        UserLocationVo userLocationVo = new UserLocationVo();
        //封装并返回
        if (!ObjectUtil.isNull(userLocation)){
            userLocationVo.setId(userLocation.getId());
            userLocationVo.setUserId(userLocation.getUserId());
            userLocationVo.setAddress(userLocation.getAddress());
            userLocationVo.setCreated(userLocation.getCreated());
            userLocationVo.setUpdated(userLocation.getUpdated());
            userLocationVo.setLastUpdated(userLocation.getLastUpdated());
            return userLocationVo;
        }
        return null;
    }

    @Override
    public void saveAndUpdate(Double longitude, Double latitude, UserLocation userLocation) {
        GeoJsonPoint point = new GeoJsonPoint(longitude, latitude);

        userLocation.setLocation(point);

        mongoTemplate.save(userLocation);
    }
}
